% function estima_out_tabs 
% =========================================================================
% ESTIMA_OUT_TABS creates 
% 1) OUTCELL  which has all info 
% 2) TOP MODE which stores max value, and saves it in directory 
% 3) SAVEPATH which is the name of the 
% 4) TOPCELL which has model identifiers, e.g. spec, subf, outfolder 
% =========================================================================
%% 4. Create Output TABS 
%
%% 4.1 ESTPAR_TAB_D Output with the ordered modes 
% -------------------------------------------------------------------------
nstrvals=size(parmodemat,2);
estpar_tab_d=emptycell(numpar+7,nstrvals+1);
estpar_tab_d(1:numpar,1)=parnames;
estpar_tab_d(numpar+2:end,1)={'logPost';'logLikel';'Number';'Iteration count';'exitflag';'minutes'};
estpar_tab_d(1:numpar,2:end)=num2cprec(parmodemat_descend, 15);
estpar_tab_d(numpar+2,2:end)=num2cprec(logpostd_mat(order_descend, 2)');
estpar_tab_d(numpar+3,2:end)=num2cprec(loglikel_mat(order_descend,2)');
estpar_tab_d(numpar+4,2:end)=num2cprec(order_descend');
estpar_tab_d(numpar+5:numpar+7, 2:end)= num2cprec(optimat_descend); 

%% 4.2 SS_TAB_D Output with associated steady states 
% -------------------------------------------------------------------------
numss = length(ssnames); 
ss_tab_d = emptycell(numss, nstrvals + 1); 
ss_tab_d(1:numss, 1) = ssnames; 
if numss >= 1
    ssvec_mat = zeros(numss, nstrvals);
else
    clear ssvec_mat
end
for ii = 1:nstrvals
    if all(parmodemat_descend(:, ii) == 0);
        continue
    end
    [GG,RR,CONS,eu,SDX,ZZ,out_temp,ssvec]=feval(funcmod,parmodemat_descend(:, ii),solveopt,addsol); 
    if isequal(eu, [1;1])
        ssvec_mat(:, ii) = ssvec;
    else
        disp(['estimated parameter ',num2str(ii), ' does not deliver SS'])
    end
end
ss_tab_d(:, 2:end) = num2cprec(ssvec_mat); 
% =========================================================================

%% 4.3 DATAINFO_TAB Information about data used in estimation 
% -------------------------------------------------------------------------
datainfo_tab = emptycell(7, nstrvals + 1); 
datainfo_tab(1, 1:2) = {'data file', data_fname};
datainfo_tab(2, 1:2) = {'data path', data_path}; 
datainfo_tab(3, 1:2) = {'data range', [num2str(dataopt.start), ' to ', num2str(dataopt.finish)]}; 
datainfo_tab(4, 1:2) = {'data estimated', [num2str(dataopt.starest), ' to ', num2str(dataopt.finishest)]};
datainfo_tab(5, 1:2) = {'demean', num2str(dataopt.demean)};
datainfo_tab(6, 1:2) = {'demean type', num2str(dataopt.demean_type)};

%% 4.4 DATASUM_TAB Names of series 
% -------------------------------------------------------------------------
datasum_tab = emptycell(nstrvals+1, size(Y, 2) + 1); 
datasum_tab{1, 1} = 'Series Names'; 
datasum_tab(1, 2:end) = Ynames; 
datasum_tab{2, 1} = 'mean';
datasum_tab(2, 2:end) = num2cprec(nanmean(Y));
datasum_tab{3, 1} = 'std dev';
datasum_tab(3, 2:end) = num2cprec(nanstd(Y));
datasum_tab = datasum_tab'; 

%% 4.5 FUNCINFO_TAB Information about solution 
% -------------------------------------------------------------------------
funcinfo_tab = emptycell(4, 2); 
funcinfo_tab(1, 1:2) = {'model', func2str(funcmod)}; 
funcinfo_tab(2, 1:2) = {'TolFun', num2str(solveopt.TolFun)};
funcinfo_tab(3, 1:2) = {'MaxIter', num2str(solveopt.MaxIter)};
funcinfo_tab(4, 1:2) = {'MaxFunEvals', num2str(solveopt.MaxFunEvals)}; 

%% 4.6 ESTINFO_TAB Information about optimization procedure 
estinfo_tab = emptycell(5, 2); 
switch case_estima
    case 1
        eststr = 'CSMINWEL';
    case 2
        eststr = 'FMINCON'; 
    case 3
        eststr = 'Giorgio SA'; 
    case 4
        eststr = 'Simulated Annealing'; 
    case 5
        eststr = 'FMINSEARCH'; 
end 
estinfo_tab(1, 1:2) = {'outpath', outpath}; 
estinfo_tab(2, 1:2) = {'routine', eststr}; 
estinfo_tab(3, 1:2) = {'TolFun', num2str(estimopt.TolFun)}; 
estinfo_tab(4, 1:2) = {'MaxIter', num2str(estimopt.MaxIter)}; 
estinfo_tab(5, 1:2) = {'MaxFunEvals', num2str(estimopt.MaxFunEvals)}; 
% =========================================================================

%% 4.7 Write OUTCELL 
mod_est_info_tab = merge_cells(estinfo_tab, funcinfo_tab, 2); 
outcell = merge_cells(mod_est_info_tab, datainfo_tab, 2); 
outcell = merge_cells(outcell, datasum_tab, 2); 
outcell = merge_cells(outcell, estpar_tab_d, 2); 
outcell = merge_cells(outcell, ss_tab_d, 2);
clear mod_est_info_tab estpar_tab_d ss_tab_d datainfo_tab; 
% =========================================================================

% =========================================================================
%% 4.8 Write values of top mode to excel
cd(outpath)
% just in case
possep = regexp(subf, '\'); 
if ~isempty(possep)
    subf = subf(possep(end):end); 
end
mode_filename=['mode ',' ',subf(2:end),' ',strdate,'.xls']; 
modecell = [parnames,  num2cprec(parmodemat_descend(:, 1), 15)]; 
if isunix == 0;
    xlswrite(mode_filename,modecell,'HighPrecision'); 
elseif isunix == 1; 
    savecell(modecell,[],outpath,mode_filename);
end
clear modecell
cd(cucd)
disp(['Saved ','mode ',spec,' ',subf]); 
disp(' '); 

save_path=cr_dir(outpath,'top mode'); 
temp      = extrsubf(loadpath, cucd ); 
if isunix == 0; 
    tempos    = findstr(temp,'\'); 
else
    tempos    = findstr(temp,'/'); 
end
root=temp(tempos(1):tempos(2)-1); 
spec=temp(tempos(2):tempos(3)-1); 
subf=temp(tempos(3):end        ); 
outfolder = extrsubf(save_path, loadpath);
topcell = {'root', root;
    'spec', spec;
    'subf', subf;
    'outfolder', outfolder
    'data', data_fname};
%clear root spec subf temp tempos; 
cd(outpath); 
outcell = merge_cells(outcell, stdcell, 2); 

cd(outpath);
if isunix==0 & size(outcell,2) < 250 
    xlswrite(load_fname,outcell,'Est Result');    
else
    savecell(outcell,[],outpath,'Est Result');
end
cd(cucd); 
disp('Added Est Results') 
% =========================================================================